package com.qtt.app.mapper;

import com.qtt.app.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository   //需要写
public interface UserMapper {
//    @Select("SELECT * FROM user")
    List<User> getAll();

//    @Insert("INSERT INTO user(phone, nickname, real_name, password, company, position) VALUES (#{phone}, #{nickname}, #{realName},#{password}, #{company}, #{position})")
//    @Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
    //以上方法没能实现，只能通过查找lastid的方法返回添加数据后的记录id
    int register(String phone,String nickname,String realName,String password,String company,String position,Date gmtCreate);

//    @Select("SELECT * FROM user WHERE user_id=#{id}")
    //查询得在xml里面写，不然大写的会在swagger显示null
    User getById(@Param("id") Long id);

    @Select("SELECT user_id FROM user ORDER BY user_id DESC LIMIT 1")
    int getLastId();//查找最后添加的id，用于添加数据后返回id

    @Select("SELECT user_id FROM user WHERE phone=#{phone} LIMIT 1")
    Long getUserByPhone(@Param("phone") String phone);

    @Delete("DELETE FROM user WHERE user_id = #{id}")
    Boolean deleteById(Long id);

    @Update("UPDATE user SET phone=#{entity.phone},password=#{entity.password},company=#{entity.company},position=#{entity.position},sex=#{entity.sex},nickname=#{entity.nickname},real_name=#{entity.realName},head=#{entity.head},gmt_modified=#{entity.gmtModified}  WHERE user_id = #{id}")
    Boolean updateById(@Param("id") Long id,User entity);

    int addUser(User entity);

    @Update("UPDATE user SET phone=#{entity.phone},openid = #{entity.openid},company=#{entity.company},position=#{entity.position},sex=#{entity.sex},nickname=#{entity.nickname},head=#{entity.head},gmt_modified=#{entity.gmtModified}  WHERE user_id = #{id}")
    Boolean cancelUser(Long id,User entity);
}